package com.zjp.mg.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.zjp.mg.core.AjaxResult;
import com.zjp.mg.model.Garage;
import com.zjp.mg.model.GarageVo;
import com.zjp.mg.service.GarageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/garage")
public class GarageController {

    @Autowired
    GarageService garageService;


    @PostMapping("save")
    public AjaxResult save() {
        for (int i = 0; i < 5; i++) {
            List<Garage> list = garageService.getGarage(i);
            if (CollUtil.isNotEmpty(list)) {
                for (Garage garage : list) {
                    String garageName = garage.getGarageName();
                    if (garageService.count(new Query(Criteria.where("garageName").is(garageName))) > 0) {
                        continue;
                    }
                    garageService.insertByAutoId(garage);
                }
            }
        }

        return AjaxResult.success();
    }


    @GetMapping("list")
    public AjaxResult list() {
        List<Garage> all = garageService.findAll();

        List<GarageVo> list = all.stream().map(GarageVo::new).collect(Collectors.toList());

        return AjaxResult.success(list);

    }


    @GetMapping("find")
    public AjaxResult find(String garageName, Double x, Double y, Double range) {

        // 设置地理位置中⼼点
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(x, y);
        // 转换为2dsphere的 距离 range单位是千米
        Distance distance = new Distance(range, Metrics.KILOMETERS);

        //画⼀个圆
        Circle circle = new Circle(geoJsonPoint, distance);

        Query query = new Query();
        if (StrUtil.isNotEmpty(garageName)) {
            query.addCriteria(Criteria.where("garageName").is(garageName));
        }

        query.addCriteria(Criteria.where("location").withinSphere(circle));

        List<Garage> garageList = garageService.find(query);
        System.out.println(garageList.size());
        Map<String, Long> map = garageList.stream().collect(Collectors.groupingBy(Garage::getGarageName, Collectors.counting()));
        System.out.println(JSONUtil.toJsonPrettyStr(map));


        NearQuery nearQuery = NearQuery.near(geoJsonPoint, Metrics.KILOMETERS).maxDistance(distance);


        GeoResults<Garage> geoResults = garageService.geoNear(nearQuery);
        int size = geoResults.getContent().size();
        System.out.println("size = " + size);

        return AjaxResult.success(garageList);
    }


}

